package com.gl.electronicVisa.fadada;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.gl.framework.config.sms.SmsConfig;
import com.gl.saasservice.entity.Department;
import com.gl.saasservice.entity.User;
import com.gl.saasservice.entity.UserDepartment;
import com.gl.saasservice.repository.DepartmentRepository;
import com.gl.saasservice.repository.QYUserRepository;
import com.gl.saasservice.repository.UserDepartmentRepository;
import com.gl.saasservice.sms.service.SmsService;
import com.haixiaoke.saas.repository.core.HttpStatus;
import com.haixiaoke.saas.repository.electronicVisa.service.FadadaElectronicVisaService;
import com.haixiaoke.saas.repository.electronicVisa.service.TencentElectronicVisaService;
import com.haixiaoke.saas.repository.electronicVisa.utils.CreateFlowUtils;
import com.haixiaoke.saas.repository.exception.BizException;
import com.tencentcloudapi.essbasic.v20210526.models.Agent;
import com.tencentcloudapi.essbasic.v20210526.models.CreateConsoleLoginUrlResponse;
import com.tencentcloudapi.essbasic.v20210526.models.UserInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author qixi
 * @date 2023/12/21
 */
@RestController
@RequestMapping("/electronicVisa/fadada")
@Slf4j
public class FadadaElectronicVisaController {


    @Autowired
    private FadadaElectronicVisaService fadadaElectronicVisaService;

    @Autowired
    private DepartmentRepository departmentRepository;
    @Autowired
    private UserDepartmentRepository  userDepartmentRepository;
    @Autowired
    private QYUserRepository userRepository;
    @Autowired
    private SmsService smsService;
    @Autowired
    private SmsConfig smsConfig;


    /**
     * 生成子客登录链接 推送链接返回给企业
     * @return
     */
    @GetMapping("/createConsoleLoginUrl/{orgId}")
    @ResponseBody
    public String createConsoleLoginUrl(@PathVariable Long orgId) {
        Optional<Department> departmentOptional = departmentRepository.findById(orgId);
        assert departmentOptional.isPresent();
        // todo 校验注册状态

        Department department = departmentOptional.get();
        String name = department.getName();
        List<UserDepartment> byDepId = userDepartmentRepository.findByDepId(department.getId());
        List<String> userIds = byDepId.stream().map(UserDepartment::getUserId).collect(Collectors.toList());
        User user = userRepository.findByIdInAndType(userIds, (short)1);
        String clientCorpId = name + "_" + user.getId() + "_" + orgId;

        String consoleLoginUrl = fadadaElectronicVisaService.getCorpAuthUrl(clientCorpId, user.getMobile());
        if (ObjectUtil.isNull(consoleLoginUrl) || StrUtil.isBlank(consoleLoginUrl)) {
            throw  new BizException(HttpStatus.ERROR, "生成企业电子签注册链接失败");
        }

        String msg = smsService.sendTemplateSmsWithData(user.getMobile(), smsConfig.getCreateConsoleLoginUrlTemplateCode(), new String[]{consoleLoginUrl});
        if ("fail".equals(msg)) {
            log.error("发送企业电子签注册链接失败");
        }
        return consoleLoginUrl;
    }

}
